package com.luculent.util.extension.datamasking.strategy;


import com.luculent.util.extension.datamasking.MaskStrategy;
import com.luculent.util.extension.datamasking.util.EncodeUtil;
import com.luculent.util.extension.datamasking.util.HashUtil;

/**
 * Hash掩码，主要用于一些敏感信息掩码后查询。
 * 算法：sha1(source+salt)
 *
 * @author zhangyang
 * @date 2020/10/30 10:58
 */
public class HashMask implements MaskStrategy {

    private static String salt = "default_salt";

    /**
     * 默认用 default_salt
     * 可以通过DataMask来设置
     *
     * @return
     */
    public static String getSalt() {
        return salt;
    }

    /**
     * 设置salt
     *
     * @param salt
     */
    public static void setSalt(String salt) {
        HashMask.salt = salt;
    }

    @Override
    public String mask(String source, int[] params) {
        if (source == null || source.isEmpty()) {
            return source;
        }
        return EncodeUtil.encodeHex(HashUtil.sha1(source + getSalt()));
    }

}
